package com.lagou.edu.demo.service.impl;

import com.lagou.edu.demo.dao.AccountDao;
import com.lagou.edu.demo.pojo.Account;
import com.lagou.edu.demo.service.TransferService;
import com.lagou.edu.myspring.annotation.MyAutowired;
import com.lagou.edu.myspring.annotation.MyService;
import com.lagou.edu.myspring.annotation.MyTransactional;

/**
 * @author zhenghao
 */
@MyService
public class TransferServiceImpl implements TransferService {
    @MyAutowired
    private AccountDao accountDao ;

    @MyTransactional
    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);

            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);

            accountDao.updateAccountByCardNo(to);
            //int c = 1/0;
            accountDao.updateAccountByCardNo(from);
    }
}
